package com.vieewer.btdemo.view.brightness

import android.content.Intent
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.util.Log
import android.view.KeyEvent
import com.vieewer.btdemo.R
import com.vieewer.btdemo.base.BaseActivity
import kotlinx.android.synthetic.main.activity_sugon_settings.*

class SugonSettingsActivity : BaseActivity() {

    private var mHasFocus: Boolean = false
    private var  mSelectBright : Boolean = false


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sugon_settings)
        setSelectedUI()
        initListeners()
    }

    private fun initListeners() {

    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        mHasFocus = hasFocus
    }


    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {

        Log.i("yjj", "TravelActivity onKeyUp >>> keyCode" + keyCode + ", action =" + event!!.action)

        when(keyCode) {

            119 -> {  //Fn按键对应键值为119

            }

            22 -> {
                if (mHasFocus) {
                    Log.i("yjj", "SugonSettingsActivity onKeyUp ")
                    mSelectBright = !mSelectBright
                    setSelectedUI()
                }
            }


            21 -> {
                if (mHasFocus) {
                    Log.i("yjj", "SugonSettingsActivity onKeyUp ")
                    mSelectBright = !mSelectBright
                    setSelectedUI()
                }

            }

        }

        return super.onKeyUp(keyCode, event)

    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {

        if (keyCode == 66 && mHasFocus) {
            gotoDetail()
        }
        return super.onKeyDown(keyCode, event)

    }

    private fun gotoDetail() {

        if (mSelectBright) {
            Log.i("yjj", "bright")
            startActivity(Intent(this, BrightnessActivity::class.java))
        } else {
            Log.i("yjj", "volume")
            startActivity(Intent(this, VolumeActivity::class.java))
        }

    }

    private fun setSelectedUI() {
        if (mSelectBright) {
            icon_brightness.setImageResource(R.drawable.icon_brightness_n)
            icon_volume.setImageResource(R.drawable.icon_volume_h)
            txt_brightness.setTextColor(ContextCompat.getColor(this, R.color.white))
            txt_volume.setTextColor(ContextCompat.getColor(this, R.color.main_color))

        } else {
            icon_brightness.setImageResource(R.drawable.icon_brightness_h)
            icon_volume.setImageResource(R.drawable.icon_volume_n)
            txt_volume.setTextColor(ContextCompat.getColor(this, R.color.white))
            txt_brightness.setTextColor(ContextCompat.getColor(this, R.color.main_color))

        }
    }










}